/*
 * Copyright (c) 2012 SUBSECRETARIA DEFENSORIA DEL ESPACIO PUBLICO - SDEP.
 * All Rights Reserved.
 * This software is the confidential and proprietary information of SDEP
 * Developed By CONSTULT-SOFT S.A, Development Team 2012.
 *
 */
package co.gov.medellin.sdep.sisdep.model.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.object.MappingSqlQuery;
import org.springframework.jdbc.object.SqlUpdate;

import co.gov.medellin.sdep.sisdep.model.ModuloDiseno;


/**
 * <p>
 * Implementacion de los metodos genericos de acceso a datos, necesarios para la gestion de Disenios de Modulos de los 
 * Comerciantes en SISDEP.
 * </p>
 * <p>
 * Copyright (c) 2012<BR>
 * Consult-Soft S.A.<BR>
 * Created on: May 09, 2012<BR>
 * 
 * @author atabares
 * </p>
 */
public class ModuloDisenoDAOImpl extends JdbcDaoSupport implements IModuloDisenoDAO {

    private InsertOperation insertOperation;

    private UpdateOperation updateOperation;

    private DeleteOperation deleteOperation;

    private DeleteByPrimaryKey deleteByPrimaryKey;

    private FindByPrimaryKeyQuery findByPrimaryKeyQuery;

    private FindByCriteriaQuery findByCriteriaQuery;

    private FindAllQuery findAllQuery;

    private FindByActiveStateQuery findByActiveStateQuery;

	private static final String SELECT = "SELECT * FROM GES_MODULO_DISENO_T ";
	private static final String WHERE = " WHERE ";
	private static final String PARAMETER = " ?, ";

    public void delete(ModuloDiseno modulodiseno) {
		deleteOperation.execute( modulodiseno  );
	}

	public void deleteByPrimaryKey(long id) {
		deleteByPrimaryKey.execute(id);
	}

	public ModuloDiseno findByPrimaryKey(long login) {
		return findByPrimaryKeyQuery.runQuery(login);
	}

	public void insert(ModuloDiseno modulodiseno) {
		insertOperation.execute( modulodiseno );
	}

	public void update(ModuloDiseno modulodiseno) {
		updateOperation.execute( modulodiseno );
	}

	public ModuloDiseno findByCriteria(ModuloDiseno  modulodiseno) {
		return findByCriteriaQuery.runQuery(modulodiseno);
	}

	public List load() {
		return findAllQuery.runQuery();
	}

	public ModuloDiseno findByActiveState(ModuloDiseno  modulodiseno) {
		return findByActiveStateQuery.runQuery(modulodiseno);
	}

	public void initDao() {
    	insertOperation   = new InsertOperation( getDataSource() );
    	updateOperation = new UpdateOperation(  getDataSource() );
    	deleteOperation = new DeleteOperation(  getDataSource() );
    	deleteByPrimaryKey = new DeleteByPrimaryKey( getDataSource());
    	findByPrimaryKeyQuery   = new FindByPrimaryKeyQuery(  getDataSource() );
    	findByCriteriaQuery = new FindByCriteriaQuery(  getDataSource() );
    	findAllQuery = new FindAllQuery( getDataSource() );
    	findByActiveStateQuery = new FindByActiveStateQuery( getDataSource() );
    }

    // ------------- SaveOperation ---------------------------------------
	class InsertOperation extends SqlUpdate	{
		private static final String sql = "INSERT INTO GES_MODULO_DISENO_T ("
				  + " ID, "
				  + " CODIGO, "
				  + " ID_MATERIAL, "
				  + " ESTILO, "
				  + " PLANO, "
				  + " ID_FOTO_ANEXO_DISENO, "
				  + " DIM_ANCHO_LARGO, "
				  + " DIM_FRENTE_FONDO_ALTO, "
				  + " DIM_FRENTE_FONDO_AREA, "
				  + " USUARIO_CREA, "
				  + " FECHA_CREA "
				  + ") VALUES ("
			  	  + " GES_MODULO_DISENO_SEC.nextVal, "
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + "SYSDATE"
				  + ")";
		public  InsertOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			compile();
		}
		public void execute(ModuloDiseno modulodiseno) {
			InsertOperation.this.update( new Object[]{
				modulodiseno.getCodigo(),
				modulodiseno.getIdMaterial(),
				modulodiseno.getEstilo(),
				modulodiseno.getPlano(),
				modulodiseno.getIdFotoAnexoDiseno(),
				modulodiseno.getDimAnchoLargo(),
				modulodiseno.getDimFrenteFondoAlto(),
				modulodiseno.getDimFrenteFondoArea(),
				modulodiseno.getUsuarioCrea()
				});
		}
    }

    // ------------- UpdateOperation -------------------------------------
    class UpdateOperation extends SqlUpdate	{
    	private static final String sql = "UPDATE GES_MODULO_DISENO_T SET "
					  + " CODIGO = ? ,"
					  + " ID_MATERIAL = ? ,"
					  + " ESTILO = ? ,"
					  + " PLANO = ? ,"
					  + " ID_FOTO_ANEXO_DISENO = ? ,"
					  + " DIM_ANCHO_LARGO = ? ,"
					  + " DIM_FRENTE_FONDO_ALTO = ? ,"
					  + " DIM_FRENTE_FONDO_AREA = ? ,"
					  + " USUARIO_MODIFICA = ? ,"
					  + " FECHA_MODIFICA = SYSDATE "
					  + " WHERE ID = ?";
		public  UpdateOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.INTEGER));
			compile();
		}
		public void execute(ModuloDiseno modulodiseno) {
			UpdateOperation.this.update( new Object[]{
				modulodiseno.getCodigo(),
				modulodiseno.getIdMaterial(),
				modulodiseno.getEstilo(),
				modulodiseno.getPlano(),
				modulodiseno.getIdFotoAnexoDiseno(),
				modulodiseno.getDimAnchoLargo(),
				modulodiseno.getDimFrenteFondoAlto(),
				modulodiseno.getDimFrenteFondoArea(),
				modulodiseno.getUsuarioModifica(),
				modulodiseno.getId(),
				});
		}
    }

    // ------------- DeleteOperation -------------------------------------
    class DeleteOperation extends SqlUpdate	{
		private static final String sql = "DELETE FROM GES_MODULO_DISENO_T "
										+ WHERE
			  	  	  					+ " CODIGO = ? ";
		public DeleteOperation(DataSource ds) {
			super(ds,sql);
			declareParameter( new SqlParameter(Types.CHAR) );
			compile();
		}
		public void execute(ModuloDiseno modulodiseno) {
			this.update( new Object[]{
				modulodiseno.getCodigo(),
			});
		}
	}

    // ------------- DeleteByPrimaryKey ----------------------------------
    class DeleteByPrimaryKey extends SqlUpdate	{
		private static final String sql = "DELETE FROM GES_MODULO_DISENO_T "
										+ " WHERE ";
		public DeleteByPrimaryKey(DataSource ds) {
			super(ds,sql);
			compile();
		}	
		public void execute(long id) {
			this.update( new Object[]{
			});
		}	
    }

    // ---- BaseQueryOperation for Mapping ResultSet values --------------
	abstract class BaseQueryOperation extends MappingSqlQuery {
		public BaseQueryOperation(DataSource ds,String sql) {
	   		super(ds,sql);
		}	
		protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
			ModuloDiseno modulodiseno = new ModuloDiseno();
				modulodiseno.setId(new Long(rs.getLong("ID")));
				modulodiseno.setCodigo(rs.getString("CODIGO"));
				modulodiseno.setIdMaterial(new Long(rs.getLong("ID_MATERIAL")));
				modulodiseno.setEstilo(rs.getString("ESTILO"));
				modulodiseno.setPlano(rs.getString("PLANO"));
				modulodiseno.setIdFotoAnexoDiseno(rs.getString("ID_FOTO_ANEXO_DISENO"));
				modulodiseno.setDimAnchoLargo(rs.getString("DIM_ANCHO_LARGO"));
				modulodiseno.setDimFrenteFondoAlto(rs.getString("DIM_FRENTE_FONDO_ALTO"));
				modulodiseno.setDimFrenteFondoArea(rs.getString("DIM_FRENTE_FONDO_AREA"));
				modulodiseno.setUsuarioCrea(rs.getString("USUARIO_CREA"));
				modulodiseno.setFechaCrea(rs.getDate("FECHA_CREA"));		
				modulodiseno.setUsuarioModifica(rs.getString("USUARIO_MODIFICA"));
				modulodiseno.setFechaModifica(rs.getDate("FECHA_MODIFICA"));		
			return modulodiseno;
		}
    }

    // ------------- FindByPrimaryKeyQuery -------------------------------
    class FindByPrimaryKeyQuery extends BaseQueryOperation {
		private static final String sql = "SELECT * FROM GES_MODULO_DISENO_T " + 
					"WHERE ID = ?";
    	
		public FindByPrimaryKeyQuery(DataSource ds) {
    		super(ds,sql);
    		declareParameter( new SqlParameter(Types.INTEGER) );
    		
			compile();
    	}
    	
    	public ModuloDiseno runQuery(long id) {
			List result =  execute( new Object[]{new Long(id)});
			
			if ( result.size() > 0 ) {
        		return (ModuloDiseno) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByCriteriaQuery ---------------------------------    
    class FindByCriteriaQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + " 1=1 ";
			//For required criteria elements uncomment required values
			  	  	  					//+ " AND ID = ? "
			  	  	  					//+ " AND CODIGO = ? "
			  	  	  					//+ " AND ID_MATERIAL = ? "
			  	  	  					//+ " AND ESTILO = ? "
			  	  	  					//+ " AND PLANO = ? "
			  	  	  					//+ " AND ID_FOTO_ANEXO_DISENO = ? "
			  	  	  					//+ " AND DIM_ANCHO_LARGO = ? "
			  	  	  					//+ " AND DIM_FRENTE_FONDO_ALTO = ? "
			  	  	  					//+ " AND DIM_FRENTE_FONDO_AREA = ? "
			  	  	  					//+ " AND USUARIO_CREA = ? "
			  	  	  					//+ " AND FECHA_CREA = ? "
			  	  	  					//+ " AND USUARIO_MODIFICA = ? "
			  	  	  					//+ " AND FECHA_MODIFICA = ? "
										//+ "";
    	public FindByCriteriaQuery(DataSource ds) {
    		super(ds,sql);
    		//For required criteria elements uncomment required values
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.DATE) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.DATE) );
			compile();
    	}
    	public ModuloDiseno runQuery(ModuloDiseno modulodiseno) {
			List result =  execute( new Object[]{
			//For required criteria elements uncomment required values
				//modulodiseno.getId(),
				//modulodiseno.getCodigo(),
				//modulodiseno.getIdMaterial(),
				//modulodiseno.getEstilo(),
				//modulodiseno.getPlano(),
				//modulodiseno.getIdFotoAnexoDiseno(),
				//modulodiseno.getDimAnchoLargo(),
				//modulodiseno.getDimFrenteFondoAlto(),
				//modulodiseno.getDimFrenteFondoArea(),
				//modulodiseno.getUsuarioCrea(),
				//modulodiseno.getFechaCrea(),
				//modulodiseno.getUsuarioModifica(),
				//modulodiseno.getFechaModifica(),
				});
			if ( result.size() > 0 ) {
        		return (ModuloDiseno) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindAllQuery ----------------------------------------    
    class FindAllQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + " 1=1 ";
    	public FindAllQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public List runQuery() {
			List result =  execute();
			if ( result.size() > 0 ) {
        		return  result;
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByActiveStateQuery  -----------------------------
    class FindByActiveStateQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ " WHERE ";
    	public FindByActiveStateQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public ModuloDiseno runQuery(ModuloDiseno modulodiseno) {
			List result =  execute( new Object[]{
			});
			if ( result.size() > 0 ) {
        		return (ModuloDiseno) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

}
